package com.hotel.core.sms;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

/**
 * @Date: 2023/10/19 11:54
 */
public class AliSmsUtil {
    private static final String ID="LTAI5t5ffSmMUa1Yf3pwPStP";
    private static final String KEY="KpVc7EAVHLhBLgDooKxABluDYaH8Aj";
    private static Client client;

    static {
        Config config = new Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(ID)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(KEY);
        config.endpoint = "dysmsapi.aliyuncs.com";
        try {
            client=new Client(config);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 发送短信验证码
     * @param phone 手机号
     * @param code 验证码*/
    public static boolean sendCode(String phone,String code){
        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName("来自邢朋辉的短信")  // 发送短信的模板不可以修改
                .setTemplateCode("SMS_115250125")
                .setTemplateParam("{\"code\":\""+code+"\"}");
        try {
            //发送短信
            SendSmsResponse response=client.sendSmsWithOptions(request, new RuntimeOptions());
            return response.body.code.equals("OK");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}